package com.zzj.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

public class Utils {

    private final static String NURSE_NUMBER_PREFIX = "HS";
    private final static String PATIENT_NUMBER_PREFIX = "HZ";

    public static MultipartFile rename(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String[] filename = originalFilename.split("\\.");
        java.io.File fileTemp= java.io.File.createTempFile(filename[0], "."+filename[1]);
        file.transferTo(fileTemp);

        fileTemp= FileUtil.rename(fileTemp, RandomUtil.randomString(10),true,true);
        InputStream inputStream  = new FileInputStream(fileTemp);
        file=new MockMultipartFile(fileTemp.getName(),inputStream);
        fileTemp.deleteOnExit();
        return file;
    }


    /**
     * 获取当前时间格式yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getNowString(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }

    /**
     *
     * @param list
     * @return
     */
    public static String listToString(List<String> list,String Split){
        String res ="";
        for (int i =0;i<list.size();i++){
            res+=list.get(i)+Split;
        }
        return res;
    }

    /**
     *
     * @param str
     * @param Split
     * @return
     */
    public static List<String> stringToList(String str, String Split){
        String[] split = str.split(",");
        return Arrays.asList(split);
    }

    /**
     * 小时
     * @param hour
     * @return
     */
    public static String getNowAddString(int hour){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date().getTime()+hour*1000*60*60);
    }
    /**
     * 时间
     * 患者
     * @return
     */
    public static String getPatientNum(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        String timestamp = dateFormat.format(new Date());
        String uniqueId = UUID.randomUUID().toString().replace("-", "");
        return PATIENT_NUMBER_PREFIX + timestamp + uniqueId.substring(0, 4);
    }
    /**
     * 时间
     * 护士工号
     * @return
     */
    public static String getNurseJobNum(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        String timestamp = dateFormat.format(new Date());
        String uniqueId = UUID.randomUUID().toString().replace("-", "");
        return NURSE_NUMBER_PREFIX + timestamp + uniqueId.substring(0, 4);
    }

    /**
     * 时间
     * 专家工号
     * @return
     */
    public static String getSpecialistNum(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String specialistNum = "ZJ" + format.format(new Date());
        return specialistNum;
    }

    /**
     * 字符串是否包含
     * @param string
     * @param str
     * @param Split
     * @return
     */
    public static boolean stringIsContainStrSplit(String string,String str,String Split){
        if(string==null)return false;
        String[] split = string.split(Split);
        List<String> list = Arrays.asList(split);
        return list.contains(str);
    }

    /**
     * 验证码生成
     * @param num
     * @return
     */
    public static String getCode(int num){
        Random random=new Random();
        String code="";
        for (int i =0;i<num;i++){
            int rand= random.nextInt(10);
            code+=rand;
        }
        return code;
    }

    /**
     * 到零点 多少秒
     * @return
     */
    public static  Long getSecondsNextEarlyMorning(){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,1);
        calendar.add(Calendar.HOUR_OF_DAY,0);
        calendar.add(Calendar.SECOND,0);
        calendar.add(Calendar.MINUTE,0);
        calendar.add(Calendar.MILLISECOND,0);
        return (calendar.getTimeInMillis()-System.currentTimeMillis())/1000;
    }
}
